import constant from '@/store/constant.js'
import common from '@/store/common.js'
const baseUrl=constant.baseUrl;
const authInfo=constant.authInfo;


export default {
	state:{//存放状态
	
		userInfo:{},
		userAmount:{},
		errorMessage:"",
		
		
		
	},
	mutations: {
		
		loadUserData(state,data){
			state.userInfo=data;
			
		},
		loadUserAmount(state,data){
			state.userAmount=data;
			
		},
		
		setErrorMessage(state,data){
			state.errorMessage=data;
		},
		
	},
	actions:{
		
		loadUserData(context,data){
		
			
			uni.request({
				url: baseUrl+'/api/userCenter/userInfo',
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data: {},
				success: (res) => {
					if(res.data.code==0){
					
						context.commit('loadUserData',res.data.result)
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
			
			
			
		},
		loadUserAmount(context,data){
			console.log("aaa:"+uni.getStorageSync("token") )
			uni.request({
				url: baseUrl+'/api/userCenter/userAmount',
				method: 'POST',
				header: {
						'auth': uni.getStorageSync("token") 
					},
				data: {},
				success: (res) => {
					if(res.data.code==0){
					
						context.commit('loadUserAmount',res.data.result)
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		editUserData(context,data){
			
			var url="";
			
		
			url=baseUrl+"/api/userCenter/editUserInfo";
		
			uni.request({
				url: url,
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data:data,
				success: (res) => {
					if(res.data.code==0){
					
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		editPwdData(context,data){
			// context.commit('setErrorMessage',"")
			var url="";
			
			console.log(JSON.stringify(data))
			
			url=baseUrl+"/api/userCenter/editPwd";
					
			uni.request({
				url: url,
				method: 'POST',
				header: {
				        'auth':uni.getStorageSync("token") 
				    },
				data:data,
				success: (res) => {
					
					console.log("success");
					console.log(res.data)
					if(res.data.code==0){
						console.log(1);
						// context.dispatch('loadUserData',{})
						
					}else{
						common.dataError(res.data);
						console.log(2);
						context.commit('setErrorMessage', res.data.message)
						
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		
		
		},
		deleteUserData(context,data){
			// context.commit('setErrorMessage',"")
			var url="";
			
			console.log(JSON.stringify(data))
			
			url=baseUrl+"/api/userCenter/deleteUser?userId="+data;
			console.log(url);
			uni.request({
				url: url,
				method: 'GET',
				header: {
				        'auth':uni.getStorageSync("token") 
				    },
				
				success: (res) => {
					
					console.log("success");
					console.log(res.data)
					if(res.data.code==0){
						console.log(1);
						// context.dispatch('loadUserData',{})
						
					}else{
						common.dataError(res.data);
						console.log(2);
						context.commit('setErrorMessage', res.data.message)
						
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		
		
		},
		
		addIncomeData(context,data){
			let url=baseUrl+"/api/userCenter/addIncome";
					
			uni.request({
				url: url,
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data:data,
				success: (res) => {
					
					console.log("success");
					console.log(res.data)
					if(res.data.code==0){
						console.log(1);
						// context.dispatch('loadUserData',{})
						
					}else{
						common.dataError(res.data);
						console.log(2);
						context.commit('setErrorMessage', res.data.message)
						
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		addExpenseData(context,data){
			let url=baseUrl+"/api/userCenter/addExpense";
					
			uni.request({
				url: url,
				method: 'POST',
				header: {
						'auth':uni.getStorageSync("token") 
					},
				data:data,
				success: (res) => {
					
					console.log("success");
					console.log(res.data)
					if(res.data.code==0){
						console.log(1);
						// context.dispatch('loadUserData',{})
						
					}else{
						common.dataError(res.data);
						console.log(2);
						context.commit('setErrorMessage', res.data.message)
						
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		}
		
		
	}
}